﻿using System;
using System.Collections.Generic;
using Microsoft.SharePoint.StsAdmin;
using System.Text;
using Microsoft.SharePoint;
using System.Collections.Specialized;
using System.IO;
using System.Globalization;

namespace Macaw.Moss2007.StsadmExtensionsSolution
{
    public class SetRegionalSettingsExtension : ISPStsadmCommand
    {

        private int updatecount = 0;

        public string GetHelpMessage(string command)
        {
            return "-url <url of web modify (rootweb or subweb)> -locale <locale to be set (nl-NL)>";
        }

        public int Run(string command, StringDictionary keyValues, out string output)
        {

            command = command.ToLowerInvariant();

            switch (command)
            {
                case "setregionalsettings":
                    return SetRegionalSettings(keyValues, out output);

                default:
                    throw new InvalidOperationException();
            }
        }

        private void SetRegionalSettingsOnWeb(SPWeb web, CultureInfo locale)
        {
            if (!web.Locale.Equals(locale))
            {
                Console.WriteLine(string.Format("setting {0} locale to {1}", web.Url, locale.DisplayName));
                web.Locale = locale;
                web.Update();
                updatecount++;
            }

            foreach (SPWeb subweb in web.Webs)
            {
                SetRegionalSettingsOnWeb(subweb, locale);
                subweb.Dispose();
            }
        }

        private int SetRegionalSettings(StringDictionary keyValues, out string output)
        {
            output = "Something went wrong!";

            if (!keyValues.ContainsKey("url"))
            {
                output = GetHelpMessage("") + "\r\nThe 'url' parameter was not specified.";
                return -1;
            }

            String url = keyValues["url"];

            if (!keyValues.ContainsKey("locale"))
            {
                output = GetHelpMessage("") + "\r\nThe 'locale' parameter was not specified.";
                return -1;
            }

            CultureInfo locale = CultureInfo.CreateSpecificCulture(keyValues["locale"]);

            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate
                {
                    using (var siteCollection = new SPSite(url))
                    {
                        using (var webSite = siteCollection.OpenWeb())
                        {
                            SetRegionalSettingsOnWeb(webSite, locale);
                        }
                        
                    }
                });
                output = "done";

                if (updatecount == 0)
                {
                    Console.WriteLine("Warning: The locale was already set correctly on the webs. So no updates were needed");
                }
                return 0;
            }
            catch (Exception ex)
            {
                output = ex.Message;
            }
            return -1;
        }

    }
}
